package logic

import (
	"context"
	"shop/goods"
	"shop/internal/svc"
	"shop/model/mysql"
	"shop/model/redis"

	"github.com/zeromicro/go-zero/core/logx"
)

type GoodsAddLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewGoodsAddLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GoodsAddLogic {
	return &GoodsAddLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *GoodsAddLogic) GoodsAdd(in *goods.GoodsAddRequest) (*goods.GoodsAddResponse, error) {
	// todo: add your logic here and delete this line
	g := mysql.Goods{
		UserId:      in.UserId,
		GoodsTitle:  in.GoodsTitle,
		GoodsNum:    int(in.GoodsNum),
		GoodsPrice:  float64(in.GoodsPrice),
		GoodsStatue: in.GoodsStatue,
	}

	err := g.Create()
	if err != nil {
		logx.Error("商品添加失败")
		return nil, err
	}

	redis.GetGoodsIdStock(int64(g.ID))

	return &goods.GoodsAddResponse{
		GoodsId: int64(g.ID),
	}, nil
}
